package com.futures.questionbank.adapter;

import android.content.Context;


import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.futures.questionbank.R;
import com.futures.questionbank.db.table.option;
import com.futures.questionbank.db.table.option_Table;
import com.futures.questionbank.db.table.problem;
import com.raizlabs.android.dbflow.sql.language.SQLite;

import java.util.List;

public class GoodsAdapter extends PagerAdapter {
    Context context;
    List<problem> ivGoodsList;
    List<option> options;
    private OptionsAdpter chapterAdpter;
    private TextView tvTitle;
    public TextView tv_title_select;
    public ViewPager viewPager;

    public GoodsAdapter(Context context, List<problem> ivGoodsList, List<option> options, ViewPager viewPager){
        this.context=context;
        this.ivGoodsList=ivGoodsList;
        this.viewPager = viewPager;
    }

    @Override
    public int getCount() {
        return ivGoodsList.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = View.inflate(context,R.layout.layout_transfer_viewpager, null);
        TextView name = (TextView) view.findViewById(R.id.tv_1);
        TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
        TextView tv_look = (TextView) view.findViewById(R.id.tv_look);
        TextView tv_2 = (TextView) view.findViewById(R.id.tv_2);
        TextView tv_num = (TextView) view.findViewById(R.id.tv_num);
        RecyclerView rv = (RecyclerView) view.findViewById(R.id.rv);
       // problem problemModel = SQLite.select().from(problem.class).where(problem_Table.exam_id.eq(ivGoodsList.get(position).getIndex())).querySingle();
         //problem  id 对应  option problem_id
        List<option> options = SQLite.select().
                from(option.class).where(option_Table.problem_id.eq(ivGoodsList.get(position).getId())).queryList();

        rv.setLayoutManager(new LinearLayoutManager(context));
        //设置Item增加、移除动画
        rv.setItemAnimator(new DefaultItemAnimator());
        name.setText(ivGoodsList.get(position).getType());
        tv_num.setText(position+1 + "/" + ivGoodsList.size());
        tvTitle.setText(ivGoodsList.get(position).getTitle());
        chapterAdpter = new OptionsAdpter(context, ivGoodsList.get(position).getMy_answer(), ivGoodsList.get(position).getType(),options, position,viewPager,new ProblemItemListener());
        rv.setAdapter(chapterAdpter);
        tv_look.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_2.setVisibility(View.VISIBLE);
                tv_2.setText(ivGoodsList.get(position).getExplain());
            }
        });
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View imageView=(View)object;
        container.removeView(imageView);
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }


    class ProblemItemListener implements BaseViewHolder.onItemCommonClickListener {
        @Override
        public void onItemClickListener(int position) {
           chapterAdpter.setCheck(position);
           notifyDataSetChanged();
        }

        @Override
        public void onItemLongClickListener(int position) {

        }
    }
}

